﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace BBPA.Admin.SYS.Goods
{
    public partial class Info : InfoPage
    {
        BLL.V_Goods bll = new BLL.V_Goods();
        protected Model.V_Goods model = new Model.V_Goods();
        //
        BLL.Pic bllPic = new BLL.Pic();
        BLL.Category bllCategory = new BLL.Category();
        BLL.Goods_BuyPrice_Log bllBuyPrice_Log = new BLL.Goods_BuyPrice_Log();
        BLL.Goods_SellPrice_Log bllSellPrice_Log = new BLL.Goods_SellPrice_Log();
        //
        protected bool isAdmin = false;
        protected void Page_Load(object sender, EventArgs e) { }
        /// <summary>
        /// 初始化本页Model
        /// </summary>
        public override void Page_Load_DefaultModel() { model = bll.GetModelList("id="+Master.id)[0];
        isAdmin = Security.GetCurrentUserType() == EnumHelper.UserType.Administrator;
        }
        /// <summary>
        /// 初始化表单
        /// </summary>
        public override void Form_Default()
        {
            List<Model.Goods_SaleWay_Discount> listModelDiscount = BLL.Goods.GetListDiscount(Master.id);
            if (listModelDiscount.Count == 0) listModelDiscount = null;
            rp_Discount.DataSource = listModelDiscount;
            rp_Discount.DataBind();

            List<Model.Goods_SaleWay_GiftNumber> listModelGiftNumber = BLL.Goods.GetListGiftNumber(Master.id);
            if (listModelGiftNumber.Count == 0) listModelGiftNumber = null;
            rp_GiftNumber.DataSource = listModelGiftNumber;
            rp_GiftNumber.DataBind();

            if (Security.InUserType(new List<EnumHelper.UserType>() { EnumHelper.UserType.Buyer, EnumHelper.UserType.Supplier }))
            {//采购员+供货商 看不到：售价
                dt_SellPrice.Visible = dd_SellPrice.Visible = false;
                if (rp_Discount .Controls.Count>0) rp_Discount.Controls[0].FindControl("th_DiscountSell").Visible = false;
                if (rp_GiftNumber.Controls.Count > 0) rp_GiftNumber.Controls[0].FindControl("th_GiftNumberSell").Visible = false;
                if (Security.GetCurrentUserType() == EnumHelper.UserType.Supplier)
                {//供应商 看不到：供货商名称
                    dt_SupplierName.Visible = dd_SupplierName.Visible = false;
                }
            }
            if (Security.GetCurrentUserType() == EnumHelper.UserType.Store)
            {//孕婴店  看不到：进价
                dt_BuyPrice.Visible = dd_BuyPrice.Visible = false;
                if (rp_Discount.Controls.Count > 0) rp_Discount.Controls[0].FindControl("th_DiscountBuy").Visible = false;
                if (rp_GiftNumber.Controls.Count > 0) rp_GiftNumber.Controls[0].FindControl("th_GiftNumberBuy").Visible = false;
            }
            
        }
        /// <summary>
        /// 绑定表单数据【修改时用】
        /// </summary>
        public override void Form_Bind()
        {
            a_Goods_BuyPrice_Log.Visible = bllBuyPrice_Log.GetRecordCount("GoodsID=" + Master.id) > 0;
            a_Goods_SellPrice_Log.Visible = bllSellPrice_Log.GetRecordCount("GoodsID=" + Master.id) > 0;
            
            rp_pic.DataSource = bllPic.GetModelList("GoodsID=" + Master.id);
            rp_pic.DataBind();

        }

        protected void rp_Discount_ItemDataBound(object sender, RepeaterItemEventArgs e)
        {
            if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)
            {
                if (Security.InUserType(new List<EnumHelper.UserType>() { EnumHelper.UserType.Buyer, EnumHelper.UserType.Supplier }))
                    e.Item.FindControl("td_DiscountSell").Visible = false;
                if (Security.GetCurrentUserType() == EnumHelper.UserType.Store) 
                    e.Item.FindControl("td_DiscountBuy").Visible = false;
                
            }
        }

        protected void rp_GiftNumber_ItemDataBound(object sender, RepeaterItemEventArgs e)
        {
            if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)
            {
                if (Security.InUserType(new List<EnumHelper.UserType>() { EnumHelper.UserType.Buyer, EnumHelper.UserType.Supplier }))
                    e.Item.FindControl("td_GiftNumberSell").Visible = false;
                if (Security.GetCurrentUserType() == EnumHelper.UserType.Store)
                    e.Item.FindControl("td_GiftNumberBuy").Visible = false;

            }
        }

    }
}